package org.gaaidou.ptarmigan.springboot.data.jpa.transcation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

@Component
public class TransactionHelper {
    @Autowired
    private PlatformTransactionManager transactionManager;

    public TransactionStatus newTransactionStatus() {
        DefaultTransactionDefinition transDef = new DefaultTransactionDefinition();
        transDef.setPropagationBehavior(DefaultTransactionDefinition.PROPAGATION_REQUIRED);
        transDef.setIsolationLevel(DefaultTransactionDefinition.ISOLATION_READ_COMMITTED);
        return transactionManager.getTransaction(transDef);
    }

    public TransactionStatus newTransactionStatus(int propagationBehavior, int isolationLevel) {
        DefaultTransactionDefinition transDef = new DefaultTransactionDefinition();
        transDef.setPropagationBehavior(propagationBehavior);
        transDef.setIsolationLevel(isolationLevel);
        return transactionManager.getTransaction(transDef);
    }

}
